<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="拣货单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="系统自动生成"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="外部单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="outerNo">
              <a-input v-model="model.outerNo" placeholder="外部接口提供"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="订单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderType">
              <j-dict-select-tag type="list" v-model="model.orderType" dictCode="wms_order_type" placeholder="请选择订单类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="订单日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderDate">
              <j-date placeholder="请选择订单订单日期"  v-model="model.orderDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="备货库位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shipLocationId">
              <j-popup
                v-model="model.shipLocationCode"
                field="shipLocationId"
                org-fields="id,coord"
                dest-fields="shipLocationId,shipLocationCode"
                code="wms_ship_location_report"
                :multi="false"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="2" style="float: right">
            <a-button icon="save" type="primary"  @click="submitForm">保 存</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="拣货单明细" key="1">
        <wms-pick-detail-list ref="WmsPickDetailList"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import WmsPickDetailList from "../WmsPickDetailList"

  export default {
    name: 'WmsPickOrderForm',
    components: {
      WmsPickDetailList
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{},
        selectObj:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          orderType:[
            { required: true, message: '请选择订单类型!' },
          ],
          shipLocationId:[
            { required: true, message: '请选择备货库位!' },
          ],
        },
        url: {
          add: "/wms/wmsPickOrder/add",
          edit: "/wms/wmsPickOrder/edit",
          queryById: "/wms/wmsPickOrder/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      popupCallback(value,row){
        this.model = Object.assign(this.model, row);
      },
      add () {
        this.visible = true;
      },
      edit (record) {
        this.selectObj = record;
        this.model = Object.assign({}, record);
        //加载子列表
        this.$nextTick(() => {
          this.$refs.WmsPickDetailList.showDetail(record,this.disabled);
        })
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.selectObj = res.result;
                that.edit(res.result);
                that.$message.success(res.message);
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              //加载子列表
              this.$nextTick(() => {
                this.$refs.WmsPickDetailList.showDetail(this.selectObj,this.disabled);
              })
              that.confirmLoading = false;
            })
          }
        })
      },
    }
  }
</script>